<?php
    function filter_title($string) {
        $searchpattern = "/.*-/i";
        preg_match($searchpattern,$string,$regs);
        $searchpattern2 = "/.*-\s/i";
        preg_match($searchpattern2,$regs[0],$regs2);
        $searchpattern3 = "/-/i";
        $newstring = preg_replace($searchpattern3, "", $regs2[0]);
        $searchpattern4 = "/[0-9]{2}\.[0-9]{2}\.[0-9]{2}/i";
        $titlestring = preg_replace($searchpattern4, "", $newstring);
        return $titlestring; 
    }
    function match_date($datum) {
        $searchpattern = "/[0-9]{2}\.[0-9]{2}\.[0-9]{2}/i";
        preg_match($searchpattern, $datum, $regs);
        return $regs[0];
    }
?>
<!DOCTYPE html>
<html>     
    <?php
        include_once './include/head.php';
    ?>
    <body> 
        <div data-role="page" id="events">
            <div data-role="header" data-position="inline">
                <div class="ui-grid-a">
                    <div class="ui-block-a">
                <div id="header">
                    <a href="index-mobile.php" data-ajax="false"><img src="themes/images/siegel.png" width="71px" height="74px" alt="TU Siegel"></a>
                </div>
                    </div>
                    <div class="ui-block-b">
                <div id="crumb">
                    <img src="themes/images/180-stickynote@2xw.png" height="36px" width="48px">
                </div>
                    </div>
                </div>
                <div style="clear: both"></div>
            </div>
            <div data-role="content" data-theme="b">
                <ul data-role="listview" data-filter="true" data-filter-placeholder="Events durchsuchen..." data-filter-theme="d" data-inset="true">
                    <?php
                    //YQL Abfrage im JSON Format
                    $yql_base_url = "http://query.yahooapis.com/v1/public/yql";
                    $yql_query = "select * from feed where url='http://www.booqtic.de/OTSPortal/welcome.faces?webid=tubs&rssfeed=881&lngid=de&search_from=today&search_to=today_p_7'";
                    $yql_query_url = $yql_base_url . "?q=" . urlencode($yql_query);
                    $yql_query_url .= "&format=json";

                    //webservice mit cURL aufrufen
                    $session = curl_init($yql_query_url);
                    curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
                    $json = curl_exec($session);
                    //json objekt in pgp objekt umwandeln
                    $phpObj = json_decode($json);

                    $events = "";
                    //Ausgabe der Events
                    if (!is_null($phpObj->query->results)) {                       
                        $i = 0;
                        foreach ($phpObj->query->results->item as $event) {  
                            $datums_array[$i] = match_date($event->title);
                            
                            if($i == 0) {
                                $events .= "<li data-role='list-divider'>".match_date($event->title) . "</li>";
                                $events .= "<li><div><h3>&nbsp;</h3><p><strong>".filter_title($event->title)."</strong><br/>".strip_tags($event->description);
                                $events .= "...<a href='$event->link' rel='external'>Details</a><br/><br/>";
                                $events .= "$event->pubDate";
                                $events .= "<a href='event_gen.php?category=".$event->category."&pubDate=".$event->pubDate."&title=".$event->title."&description=".strip_tags($event->description)."' rel='external'> Kalender Import</a></p></div>";
                                $events .= "<p class='ui-li-aside'>Kategorie: <strong>".$event->category."</strong></p>";        
                            }
                            else if(match_date($event->title) != $datums_array[$i-1]) {
                                $events .= "</li><li data-role='list-divider'>".match_date($event->title) . "</li>";
                                $events .= "<li><div><h3>&nbsp;</h3><p><strong>".filter_title($event->title)."</strong><br/>".strip_tags($event->description);
                                $events .= "...<a href='$event->link' rel='external'>Details</a><br/><br/>";                                
                                $events .= "$event->pubDate";                            
                                $events .= "<a href='event_gen.php?category=".$event->category."&pubDate=".$event->pubDate."&title=".$event->title."&description=".strip_tags($event->description)."' rel='external'> Kalender Import</a></p></div>";
                                $events .= "<p class='ui-li-aside'>Kategorie: <strong>".$event->category."</strong></p>";      
                            }
                            else {
                                $events .= "<li><div><h3>&nbsp;</h3><p><strong>".filter_title($event->title)."</strong><br/>";
                                $events .= strip_tags($event->description);
                                $events .= "...<a href='$event->link' rel='external'>Details</a><br/><br/>";
                                $events .= "$event->pubDate";                            
                                $events .= "<a href='event_gen.php?category=".$event->category."&pubDate=".$event->pubDate."&title=".$event->title."&description=".strip_tags($event->description)."' rel='external'> Kalender Import</a></p>";
                                $events .= "<p class='ui-li-aside'>Kategorie: <strong>".$event->category."</strong></p></div>"; 
                            }
                            $i += 1;
                        }
                    }
                    echo $events;                    
?>
                </ul>
            </div>

        </div>
    </body>
</html>